package com.erp.sell.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.erp.common.utils.poi.ExcelUtil;
import com.erp.sell.domain.ErpSalesOrderDTO;
import com.erp.sell.domain.ErpSalesOrderVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.erp.common.annotation.Log;
import com.erp.common.core.controller.BaseController;
import com.erp.common.core.domain.AjaxResult;
import com.erp.common.enums.BusinessType;
import com.erp.sell.domain.ErpSalesOrder;
import com.erp.sell.service.IErpSalesOrderService;
import com.erp.common.core.page.TableDataInfo;

/**
 * 销售订单Controller
 *
 * @author lzx
 * @date 2025-09-02
 */
@RestController
@RequestMapping("/sell/salesOrder")
public class ErpSalesOrderController extends BaseController
{
    @Autowired
    private IErpSalesOrderService erpSalesOrderService;

    /**
     * 查询销售订单列表
     * @param erpSalesOrder 销售订单
     * @return 销售订单集合
     */
    @PreAuthorize("@ss.hasPermi('sell:salesOrder:list') or @ss.hasPermi('sell:salesOrder:query')")
    @GetMapping("/list")
    public TableDataInfo list(ErpSalesOrder erpSalesOrder)
    {
        startPage();
        List<ErpSalesOrderVO> list = erpSalesOrderService.selectErpSalesOrderList(erpSalesOrder);
        TableDataInfo dataTable = getDataTable(list);
        // 更新订单数量
        dataTable.setTotal(count(erpSalesOrder));
        return dataTable;
    }

    /**
     * 销售订单数量
     * @return 订单数量
     */
    public int count(ErpSalesOrder erpSalesOrder) {
        return erpSalesOrderService.count(erpSalesOrder);
    }

    /**
     * 导出销售订单列表
     */
    @PreAuthorize("@ss.hasPermi('sell:salesOrder:export')")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpSalesOrder erpSalesOrder)
    {
        List<ErpSalesOrderVO> list = erpSalesOrderService.selectErpSalesOrderList(erpSalesOrder);
        ExcelUtil<ErpSalesOrderVO> util = new ExcelUtil<>(ErpSalesOrderVO.class);
        util.exportExcel(response, list, "销售订单数据");
    }

    /**
     * 新增销售订单
     * 新增Service层调用多个Mapper接口,此处Controller层添加了事务
     */
    @PreAuthorize("@ss.hasPermi('sell:salesOrder:add')")
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody ErpSalesOrderDTO erpSalesOrderDTO)
    {
        logger.debug("新增销售订单:{}", erpSalesOrderDTO);
        return toAjax(erpSalesOrderService.insertErpSalesOrder(erpSalesOrderDTO));
    }

    /**
     * 修改销售订单
     */
    @PreAuthorize("@ss.hasPermi('sell:salesOrder:edit')")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @GetMapping("/edit/{orderId}")
    public AjaxResult edit(@PathVariable int orderId)
    {
        return toAjax(erpSalesOrderService.updateErpSalesOrder(orderId));
    }

    /**
     * 删除销售订单
     */
    @PreAuthorize("@ss.hasPermi('sell:salesOrder:remove')")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
	@DeleteMapping("/{orderId}")
    public AjaxResult remove(@PathVariable int orderId)
    {
        return toAjax(erpSalesOrderService.deleteErpSalesOrderByOrderId(orderId));
    }

    /**
     * 获取订单状态统计
     */
    @PreAuthorize("@ss.hasPermi('sell:salesOrder:list')")
    @GetMapping("/statistics")
    public AjaxResult getOrderStatusStatistics()
    {
        List<Map<String, Object>> statistics = erpSalesOrderService.getOrderStatusStatistics();
        return AjaxResult.success(statistics);
    }
}
